# -*- coding:utf-8 -*-

from typing import List
from sqlalchemy.ext.asyncio import AsyncSession
from utils.common_util import CamelCaseUtil, export_list2excel
from module_admin.entity.vo.sys_table_vo import SysTablePageModel
from module_admin.service.sys_table_service import SysTableService
from utils.page_util import PageResponseModel
from module_stock.dao.stock_basic_dao import StockBasicDao
from module_stock.entity.do.stock_basic_do import StockBasic
from module_stock.entity.vo.stock_basic_vo import StockBasicPageModel, StockBasicModel


class StockBasicService:
    """
    用户管理模块服务层
    """

    @classmethod
    async def get_stock_basic_list(cls, query_db: AsyncSession, query_object: StockBasicPageModel, data_scope_sql: str) -> [list | PageResponseModel]:
        stock_basic_list = await StockBasicDao.get_stock_basic_list(query_db, query_object, data_scope_sql, is_page=True)
        return stock_basic_list
    @classmethod
    async def get_stock_basic_list_nopage(cls, query_db: AsyncSession, query_object: StockBasicPageModel, data_scope_sql: str) -> [list | PageResponseModel]:
        stock_basic_list = await StockBasicDao.get_stock_basic_list(query_db, query_object, data_scope_sql)
        return stock_basic_list
    @classmethod
    async def get_stock_basic_by_id(cls, query_db: AsyncSession, stock_basic_id: int) -> StockBasicModel:
        stock_basic = await  StockBasicDao.get_by_id(query_db, stock_basic_id)
        stock_basic_model = StockBasicModel(**CamelCaseUtil.transform_result(stock_basic))
        return stock_basic_model


    @classmethod
    async def add_stock_basic(cls, query_db: AsyncSession, query_object: StockBasicModel) -> StockBasicModel:
        stock_basic = await StockBasicDao.add_stock_basic(query_db, query_object)
        stock_basic_model = StockBasicModel(**CamelCaseUtil.transform_result(stock_basic))
        return stock_basic_model


    @classmethod
    async def update_stock_basic(cls, query_db: AsyncSession, query_object: StockBasicModel) -> StockBasicModel:
        stock_basic = await StockBasicDao.edit_stock_basic(query_db, query_object)
        stock_basic_model = StockBasicModel(**CamelCaseUtil.transform_result(stock_basic))
        return stock_basic_model


    @classmethod
    async def del_stock_basic(cls, query_db: AsyncSession, stock_basic_ids: List[str]):
        await StockBasicDao.del_stock_basic(query_db, stock_basic_ids)


    @classmethod
    async def export_stock_basic_list(cls, query_db: AsyncSession, query_object: StockBasicPageModel, data_scope_sql) -> bytes:
        stock_basic_list = await StockBasicDao.get_stock_basic_list(query_db, query_object, data_scope_sql, is_page=False)
        filed_list = await SysTableService.get_sys_table_list(query_db, SysTablePageModel(tableName='stock_basic'), is_page=False)
        filtered_filed = sorted(filter(lambda x: x["show"] == '1', filed_list), key=lambda x: x["sequence"])
        new_data = []
        for item in stock_basic_list:
            mapping_dict = {}
            for fild in filtered_filed:
                if fild["prop"] in item:
                    mapping_dict[fild["label"]] = item[fild["prop"]]
            new_data.append(mapping_dict)
        binary_data = export_list2excel(new_data)
        return binary_data